package labox.innovation.gameserver.model.itemcontainer;

import java.util.List;

import javolution.util.FastList;
import labox.innovation.config.Config;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.L2ItemInstance.ItemLocation;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;

public class PcFreight extends ItemContainer {
	//private static final Logger _log = Logger.getLogger(PcFreight.class.getName());

	private FuzePcInstance _owner; // This is the L2PcInstance that owns this Freight;

	private int _activeLocationId;

	private int _tempOwnerId = 0;

	public PcFreight(FuzePcInstance owner) {
		_owner = owner;
	}

	public String getName() {
		return "Freight";
	}

	@Override
	public FuzePcInstance getOwner() {
		return _owner;
	}

	@Override
	public ItemLocation getBaseLocation() {
		return ItemLocation.FREIGHT;
	}

	public void setActiveLocation(int locationId) {
		_activeLocationId = locationId;
	}

	/**
	 * Returns the quantity of items in the inventory
	 * @return int
	 */
	@Override
	public int getSize() {
		int size = 0;
		for (L2ItemInstance item : _items) {
			if (item.getLocationSlot() == 0 || _activeLocationId == 0 || item.getLocationSlot() == _activeLocationId)
				size++;
		}
		return size;
	}

	/**
	 * Returns the list of items in inventory
	 * @return L2ItemInstance : items in inventory
	 */
	@Override
	public L2ItemInstance[] getItems() {
		List<L2ItemInstance> list = new FastList<L2ItemInstance>();
		for (L2ItemInstance item : _items) {
			if (item.getLocationSlot() == 0 || item.getLocationSlot() == _activeLocationId)
				list.add(item);
		}

		return list.toArray(new L2ItemInstance[list.size()]);
	}

	/**
	 * Returns the item from inventory by using its <B>itemId</B>
	 * @param itemId : int designating the ID of the item
	 * @return L2ItemInstance designating the item or null if not found in inventory
	 */
	@Override
	public L2ItemInstance getItemByItemId(int itemId) {
		for (L2ItemInstance item : _items)
			if ((item.getItemId() == itemId) && (item.getLocationSlot() == 0 || _activeLocationId == 0 || item.getLocationSlot() == _activeLocationId))
				return item;

		return null;
	}

	/**
	 * Adds item to PcFreight for further adjustments.
	 * @param item : L2ItemInstance to be added from inventory
	 */
	@Override
	protected void addItem(L2ItemInstance item) {
		super.addItem(item);
		if (_activeLocationId > 0)
			item.setLocation(item.getLocation(), _activeLocationId);
	}

	/**
	 * Get back items in PcFreight from database
	 */
//	@Override
//	public void restore() {
//		int locationId = _activeLocationId;
//		_activeLocationId = 0;
//		super.restore();
//		_activeLocationId = locationId;
//	}

//	@Override
//	public boolean validateCapacity(int slots) {
//		int cap = (_owner == null ? Config.FREIGHT_SLOTS : _owner.getFreightLimit());
//
//		return (getSize() + slots <= cap);
//	}

	@Override
	public long getOwnerId() {
		if (_owner == null)
			return _tempOwnerId;

		return super.getOwnerId();
	}

	/**
	 * This provides support to load a new PcFreight without owner so that transactions can be done
	 */
	public void doQuickRestore(int val) {
		_tempOwnerId = val;
//		restore();
	}
	/***
	*没有实现
    */
	@Override
	public int getMaxCurrentSoltNums() {
		// TODO Auto-generated method stub
		return 0;
	}
	/***
	*没有实现
    */
	@Override
	public void setMaxCurrentSoltNums(int maxCurrentSoltNums) {
		// TODO Auto-generated method stub
		
	}
}